package cz.muni.fi.pa165.languageschool.utils;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * Adapter for converting Time representation and String representation of time
 * values used within upper application layers
 *
 * @author Michal Fucik (395624) michal.fuca.fucik(at)gmail.com
 */
public class TimeAdapter extends XmlAdapter<String, Time> {

    /**
     * Unmarshals string date representation into DB Time representation
     *
     * @param val
     * @return
     * @throws Exception
     */
    @Override
    public Time unmarshal(String val) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat();
        Date d = sdf.parse(val);
        return new Time(d.getTime());
    }

    /**
     * Marshals Time into String representation used within upper layers
     *
     * @param val
     * @return
     * @throws Exception
     */
    @Override
    public String marshal(Time val) throws Exception {
        return val.toString();
    }
}
